package org.alerve.android.resourcebuilder.modules;

import org.alerve.android.resourcebuilder.IModule;
import org.alerve.android.resourcebuilder.Layout;
import org.alerve.android.resourcebuilder.AbstractDirector.ModuleClass;

import android.app.Activity;
import android.view.InflateException;
import android.view.View;

/**
 * 
 * Concrete implementation of {@link IModule} for {@link Activity} instances
 * 
 * @author pabs87@gmail.com
 * 
 */
@ModuleClass(Activity.class)
public class ActivityModule extends DecoratorModule<Activity> {
	
	public ActivityModule() {
		super();
		addModule(new ActivityInflator());
		addModule(new ActivityPopulator());
	}

	private final class ActivityInflator extends InflatorModule<Activity> {

		@Override
		public boolean inflate(Activity subject, Layout layout) {
			if (layout != null) {
				subject.setContentView(layout.value());
				return true;
			} else {
				throw new InflateException("The activity: "
						+ subject.getClass().getName() + " must have a layout");
			}
		}
	}
	
	private final class ActivityPopulator extends PopulatorModule<Activity> {
		
		@Override
		protected View findViewById(Activity parent, int id) {
			return parent.findViewById(id);
		}
	}

}
